<template>
    <nav :class="'site-navbar--' + navbarLayoutType" class="site-navbar">
        <div class="site-navbar__header">
            <h1 @click="$router.push({ name: 'home' })" class="site-navbar__brand">
                <a class="site-navbar__brand-lg" href="javascript:;">mall4j建站后台</a>
                <a class="site-navbar__brand-mini" href="javascript:;">mall4j</a>
            </h1>
        </div>
        <div class="site-navbar__body clearfix">
            <el-menu
                    class="site-navbar__menu"
                    mode="horizontal">
                <el-menu-item @click="sidebarFold = !sidebarFold" class="site-navbar__switch" index="0">
                    <icon-svg name="zhedie"></icon-svg>
                </el-menu-item>
            </el-menu>
            <el-menu
                    class="site-navbar__menu site-navbar__menu--right"
                    mode="horizontal">
                <el-menu-item class="site-navbar__avatar" index="3">
                    <el-dropdown :show-timeout="0" placement="bottom">
            <span class="el-dropdown-link">
              {{ userName }}
            </span>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="updatePasswordHandle()">修改密码</el-dropdown-item>
                            <el-dropdown-item @click.native="logoutHandle()">退出</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </el-menu-item>
            </el-menu>
        </div>
        <!-- 弹窗, 修改密码 -->
        <update-password ref="updatePassowrd" v-if="updatePassowrdVisible"></update-password>
    </nav>
</template>

<script>
    import UpdatePassword from './main-navbar-update-password'
    import {clearLoginInfo} from '@/utils'

    export default {
        data() {
            return {
                updatePassowrdVisible: false
            }
        },
        components: {
            UpdatePassword
        },
        computed: {
            navbarLayoutType: {
                get() {
                    return this.$store.state.common.navbarLayoutType
                }
            },
            sidebarFold: {
                get() {
                    return this.$store.state.common.sidebarFold
                },
                set(val) {
                    this.$store.commit('common/updateSidebarFold', val)
                }
            },
            mainTabs: {
                get() {
                    return this.$store.state.common.mainTabs
                },
                set(val) {
                    this.$store.commit('common/updateMainTabs', val)
                }
            },
            userName: {
                get() {
                    return this.$store.state.user.name
                }
            }
        },
        methods: {
            // 修改密码
            updatePasswordHandle() {
                this.updatePassowrdVisible = true
                this.$nextTick(() => {
                    this.$refs.updatePassowrd.init()
                })
            },
            // 退出
            logoutHandle() {
                this.$confirm(`确定进行[退出]操作?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http({
                        url: this.$http.adornUrl('/logOut'),
                        method: 'post',
                        data: this.$http.adornData()
                    }).then(({data}) => {
                        clearLoginInfo()
                        this.$router.push({name: 'login'})
                    })
                }).catch(() => {
                })
            }
        }
    }
</script>
